package com.gljx.spring.manage.resolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.gljx.spring.manage.exception.MyException;


/**
 * spring异常处理
* @author LiXiang
* @date 2017年10月12日 上午10:47:11 
*
 */
public class MyExceptionResolver implements HandlerExceptionResolver {
	
	private  Logger logger = Logger.getLogger(getClass());
	
	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, 
			Object object,
			Exception e) {
		
		ModelAndView modelAndView = new ModelAndView();
		//自定义异常类别
		if(e instanceof MyException){
			MyException myException= (MyException)e;
			modelAndView.addObject("error", myException.getMsg());
			logger.info("#自定义异常--说明：#"+myException.getMsg()+"#");
		}
		if(e instanceof org.apache.shiro.authz.AuthorizationException){
			modelAndView.addObject("error", "权限不足！！！");
			modelAndView.setViewName("error");
			logger.info("#shiro权限检查--说明：#权限不足！！！#");
			return modelAndView;
		}
		else{
			modelAndView.addObject("error", "未知异常！！！！！！！！");
			logger.info("#其他异常--说明：");
			e.printStackTrace();
		}
		modelAndView.setViewName("error");
		return modelAndView;
	}

}
